package com.sugon.controller.tool;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.ToolsCheckTicketEntity;
import com.sugon.entity.ToolsCheckTicketItemEntity;
import com.sugon.modules.device.consts.ToolsCheckTicketState;
import com.sugon.modules.tools.service.IToolsCheckTicketManager;
import com.sugon.service.ToolsCheckTicketItemService;
import com.sugon.service.ToolsCheckTicketService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测工单明细Controller
 *
 * @author sugon
 * @date 2021-09-27 10:47:49
 */
@Controller
@RequestMapping("toolscheckticketitem")
public class ToolsCheckTicketItemController {
    @Autowired
    private ToolsCheckTicketItemService toolsCheckTicketItemService;
    @Autowired
    private IToolsCheckTicketManager toolsCheckTicketManager;
    @Autowired
    private ToolsCheckTicketService toolsCheckTicketService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("toolscheckticketitem:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ToolsCheckTicketItemEntity> toolsCheckTicketItemList = toolsCheckTicketItemService.queryList(query);
        int total = toolsCheckTicketItemService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsCheckTicketItemList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/handle")
    @RequiresPermissions("toolscheckticketitem:handle")
    @ResponseBody
    public R handle(@RequestBody ToolsCheckTicketItemEntity toolsCheckTicketItem) {
        return toolsCheckTicketItemService.addInvalidHandleRecord(toolsCheckTicketItem) ? R.ok() : R.error("处置失败，请稍后重试！");
    }

    @RequestMapping("/commit")
    @RequiresPermissions("toolscheckticketitem:commit")
    @ResponseBody
    public R commit(@RequestParam String toolsCheckTicketId) {
        // 校验工装周期性检测工单是否存在
        ToolsCheckTicketEntity toolsCheckTicket = toolsCheckTicketService.queryObject(toolsCheckTicketId);
        if (toolsCheckTicket == null) {
            return R.error("工装周期性检测工单不存在，请确认！");
        }
        // 校验状态是否合法
        if (!ToolsCheckTicketState.ING.equals(toolsCheckTicket.getState())) {
            return R.error("测量数据已提交或工单已关闭，请勿重复提交！");
        }
        // 标识工单为已完成
        toolsCheckTicket.setState(ToolsCheckTicketState.ED);
        toolsCheckTicket.setExecutor(ShiroUtils.getUserId());
        return toolsCheckTicketService.commitMeasureData(toolsCheckTicket) ? R.ok() : R.error();
    }

    @RequestMapping("/updateMeasuredValue")
    //@RequiresPermissions("toolscheckticketitem:update")
    @ResponseBody
    public R updateMeasuredValue(ToolsCheckTicketItemEntity toolsCheckTicketItem) {
        return toolsCheckTicketManager.updateMeasuredValue(toolsCheckTicketItem) ? R.ok() : R.error("更新失败");
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsCheckTicketItemEntity> list = toolsCheckTicketItemService.queryList(params);

        return R.ok().put("list", list);
    }
}
